Terrain.STATUS_WORD = 1;
Terrain.STATUS_CHAR = 2;

Terrain.ID = 0;

function Terrain(x, y, width, height, element, elementX, elementY, elementWidth, elementHeight, elementClip, status, indestructible, forceOutside) {
	this.id = Terrain.ID++;
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	this.element = element;
	this.elementX = elementX;
	this.elementY = elementY;
	this.elementWidth = elementWidth;
	this.elementHeight = elementHeight;
	this.elementClip = elementClip;
	this.status = status;
	this.health = 200;
	this.destroyed = false;
	this.forceOutside = forceOutside;
	this.indestructible = indestructible;
	if (element && !element.dcChildCount)
		element.dcChildCount = 0;

	this.destroy = function() {
		if (this.destroyed)
			console.log("already destroyed!!!!!!!!");
		this.element.dcChildCount -= 1;
		if (this.element.dcChildCount <= 0) {
			this.element.style.visibility = 'hidden';
		}
		this.destroyed = true;
	};

	this.draw = function(ctx) {
		if (!this.destroyed && DEBUG_COLLISIONS) {
			ctx.strokeStyle = "rgba(0,200,200,0.5)";
			ctx.strokeRect(this.x, this.y, this.width, this.height);
		}

		if (this.destroyed && this.element.dcChildCount > 0) {
			ctx.fillStyle = "rgba(255,255,255,1)";
			ctx.fillRect(this.x, this.y, this.width, this.height);
		}
	};
}

function FallingTerrain(terrain, fallingDiv) {
	this.count = 0;
	this.xoff = 0;
	this.yoff = 0;
	this.rotation = 0;
	this.rotationalvel = Math.random() * 40 - 20;
	this.xvel = Math.random() * 20 - 10;
	this.yvel = (Math.random() * -20) - 8;
	this.element;
	this.terrain = terrain;
	this.fallingDiv = fallingDiv;
	this.xorigin;
	this.yorigin;
	this.xpos;
	this.ypos;
	this.destroyed = false;

	this.init = function() {
		this.element = this.terrain.element.cloneNode(true);
		this.fallingDiv.appendChild(this.element);
		pos = $(this.terrain.element).offset();
		this.xorigin = pos.left;
		this.yorigin = pos.top;
		$(this.element).offset(pos);
		this.element.style.position = 'absolute';
		this.element.style.color = $(this.terrain.element).css('color');
		this.element.style.textDecoration = $(this.terrain.element).css('text-decoration');
		this.element.style.fontSize = $(this.terrain.element).css('font-size');
		this.element.style.fontWeight = $(this.terrain.element).css('font-weight');
		this.element.style.backgroundColor = $(this.terrain.element).css('background-color');
//		this.element.style.width = $(this.terrain.element).css('width');
//		this.element.style.height = $(this.terrain.element).css('height');
		var offsetX = this.terrain.x - this.terrain.elementX;
		var offsetY = this.terrain.y - this.terrain.elementY;
		console.log(offsetX + " " + offsetY);
		if (this.terrain.elementClip)
			this.element.style.clip = "rect(" + offsetY + "px " + (offsetX + this.terrain.width) + "px " + (offsetY + this.terrain.height) + "px " + offsetX
					+ "px)";
		

	};

	this.step = function() {
		this.yvel += 1;
		this.yoff += this.yvel;
		this.xoff += this.xvel;
		rotateString = 'rotate(' + this.rotation + 'deg)';
		translateString = "translate(" + this.xoff + "px," + this.yoff + "px)";
		var offsetX = this.terrain.x - this.terrain.elementX;
		var offsetY = this.terrain.y - this.terrain.elementY;
		var originX = offsetX + (0.5 * this.terrain.width);
		var originY = offsetY + (0.5 * this.terrain.height);
		$(this.element).css('-webkit-transform-origin', originX + "px " + originY + "px");
		$(this.element).css('transform', translateString + " " + rotateString);
		this.rotation += this.rotationalvel;
		this.xpos = this.xorigin + this.xoff;
		this.ypos = this.yorigin + this.yoff;
	};

	this.outsideOf = function(xmin, ymin, xmax, ymax) {
		if (this.xpos < xmin || this.xpos > xmax || this.ypos > ymax) {
			return true;
		}
		return false;
	};

	this.destroy = function() {
		if (this.destroyed)
			console.log("already destroyed!!!!!!!!");
		this.fallingDiv.removeChild(this.element);
		this.destroyed = true;
	};

	this.init();

}